package cn.yedong.basic.controller;

import cn.yedong.basic.dto.LoginDto;
import cn.yedong.basic.exception.BusinessException;
import cn.yedong.basic.util.AjaxResult;
import cn.yedong.user.mapper.LogininfoMapper;
import cn.yedong.user.service.ILogininfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@RequestMapping("/login")
public class LoginController {
    @Autowired
    private ILogininfoService logininfoService;

    /**
     * 账号登陆：前后台共用
     * @param loginDto
     * @return
     */
    @PostMapping("/account")
    public AjaxResult loginAccount(@RequestBody LoginDto loginDto){
        try {
            Map<String,Object> map= logininfoService.loginAccount(loginDto);
            return AjaxResult.me().setObj(map);
        } catch (BusinessException e) {
            return new AjaxResult(false,e.getMessage());
        }catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"亲，网络繁忙，请稍后再试！");
        }
    }

    /**
     * 微信登陆登陆：前台
     * @param map
     * @return
     */
    @PostMapping("/wechat")
    public AjaxResult loginWechat(@RequestBody Map<String,String> map){
        try {
            return logininfoService.loginWechat(map);
        } catch (BusinessException e) {
            return new AjaxResult(false,e.getMessage());
        }catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"后：亲，网络繁忙，请稍后再试！");
        }
    }

    /**
     * 微信绑定：前台
     * @param map
     * @return
     */
    @PostMapping("/wechat/binder")
    public AjaxResult wechatBinder(@RequestBody Map<String,String> map){
        try {
            return logininfoService.wechatBinder(map);
        } catch (BusinessException e) {
            return new AjaxResult(false,e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"亲，网络忙，请稍后再试！");
        }
    }
}
